
// ChildFrm.cpp : implementation of the COpenCVFrm class
//

#include "stdafx.h"
#include "OpenCVView.h"
#include "OpenCVApp.h"
#include "OpenCVFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// COpenCVFrm

static int gCreateNum = 0;
IMPLEMENT_DYNCREATE(COpenCVFrm, CMDIChildWnd)

BEGIN_MESSAGE_MAP(COpenCVFrm, CMDIChildWnd)
	//ON_WM_SIZE()
	//ON_WM_CREATE()
	//ON_WM_CLOSE()
	ON_WM_CREATE()
	ON_WM_SIZE()
END_MESSAGE_MAP()

// COpenCVFrm construction/destruction

COpenCVFrm::COpenCVFrm()
{
	// TODO: add member initialization code here
	++gCreateNum;
}

COpenCVFrm::~COpenCVFrm()
{
	--gCreateNum;
}


BOOL COpenCVFrm::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying the CREATESTRUCT cs
	if( !CMDIChildWnd::PreCreateWindow(cs) )
		return FALSE;

	return TRUE;
}

// COpenCVFrm diagnostics

#ifdef _DEBUG
void COpenCVFrm::AssertValid() const
{
	CMDIChildWnd::AssertValid();
}

void COpenCVFrm::Dump(CDumpContext& dc) const
{
	CMDIChildWnd::Dump(dc);
}
#endif //_DEBUG

// COpenCVFrm message handlers

int COpenCVFrm::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CXtremChildFrame::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  Add your specialized creation code here

	return 0;
}


void COpenCVFrm::OnSize(UINT nType, int cx, int cy)
{
	CXtremChildFrame::OnSize(nType, cx, cy);

	// TODO: Add your message handler code here
}



bool COpenCVFrm::CreateRibbonBar(void)
{
	CXtremeMDIFrameWnd* pMainFrame = (CXtremeMDIFrameWnd*)AfxGetMainWnd();	
	CSysLocalResource res(theApp.m_hInstance);	
	CXTPCommandBars* pCommandBars = pMainFrame->GetCommandBars();
	if(gCreateNum > 1)
	{
		int count = pRibbonBar->GetTabCount();
		for(int i = 1 ; i< count; ++i)
		{
			CXTPRibbonTab* pTab = pRibbonBar->GetTab(i);
			if(pTab == pTabControl)
			{
				pRibbonBar->SetCurSel(i);
			}
		}
		return true;
	}
	pRibbonBar = (CXTPRibbonBar*)pCommandBars->GetAt(0);
	if (!pRibbonBar)
	{
		return false;
	}
	
	CXTPControl* pControl = NULL;
	pTabControl = pRibbonBar->AddTab(ID_OPEN_EXAMPLE);
	CXTPRibbonGroup* pGroupFile = pTabControl->AddGroup(ID_MEDIA_ORDER);	
	pGroupFile->Add(xtpControlButton, ID_OPEN_AVI);	
	pGroupFile->Add(xtpControlButton, ID_OPEN_FILE);
	pGroupFile->Add(xtpControlButton, ID_MEDIA_CAM);
	pGroupFile->Add(xtpControlButton, ID_CLOSE_FILE);
	pControl = pGroupFile->Add(xtpControlButton, ID_MEDIA_PLAY);
	pControl->SetBeginGroup(TRUE);
	//pGroupFile->Add(xtpControlButton, ID_MEDIA_PAUSE);
	//pGroupFile->Add(xtpControlButton, ID_MEDIA_RESTORE);
	pGroupFile->Add(xtpControlButton, ID_MEDIA_STOP);
	pControl = pControl = (CXTPControl*)pGroupFile->Add(xtpControlButton, ID_MEDIA_SHOW);
	pControl->SetBeginGroup(TRUE);
	//pControl->SetBeginGroup(TRUE);
	//pGroupFile->Add(xtpControlButton, ID_MEDIA_SCALE);
	CXTPControlPopup* pControlPopup = NULL;
	pControlPopup = (CXTPControlPopup*)pGroupFile->Add(xtpControlSplitButtonPopup, ID_MEDIA_SCALE);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_NARROW_1);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_NARROW_2);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_NARROW_3);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_NARROW_4);
	pControlPopup->SetBeginGroup(TRUE);




	pGroupFile = pTabControl->AddGroup(ID_MEDIA_DEALWITH);	

	pControlPopup = (CXTPControlPopup*)pGroupFile->Add(xtpControlSplitButtonPopup, ID_MEDIA_POINT);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_COLOR_TO_GRAY);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_IMAGE_INVERT);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_FLIP_V);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_FLIP_H);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_FLIP);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_ROTATION_30);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_WARP_AFFINE);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_WARP_PERSPECT);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_IMAGE_ADJUST);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_IMAGE_HISTOGRAM);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_HIST_EQUALIZE);
	pControlPopup->SetBeginGroup(TRUE);
	
	pControlPopup = (CXTPControlPopup*)pGroupFile->Add(xtpControlSplitButtonPopup, ID_MEDIA_REGION);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_BLUR_SMOOTH);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_GAUSS_SMOOTH);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_MEDIAN_SMOOTH);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_SOBEL);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_LAPLACE);
	pControlPopup->SetBeginGroup(TRUE);
	
	pControlPopup = (CXTPControlPopup*)pGroupFile->Add(xtpControlSplitButtonPopup, ID_MEDIA_TVALUE);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_THRESHOLDING_1);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_ADAPTIVE_THRESHOLD);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_BASIC_GLOBAL_THRESHOLD);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_MAXENTROPY_THRESHOLD);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_OTSU_THRESHOLD);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_UPDOWN_THRESHOLD);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_DETECT_THRESHOLD);
	pControlPopup->SetBeginGroup(TRUE);
	
	pControlPopup = (CXTPControlPopup*)pGroupFile->Add(xtpControlSplitButtonPopup, ID_MEDIA_DEAL);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_MIN_AREA_RECT_2);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_AREA_RECT);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_MIN_AREA_RECT);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_APPROX_POLY);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_CONVEX_HULL_2);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_CONVEX_HULL);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_AREA_CONVEX_HULL);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_CONVEXITY_DEFECTS);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_CONTOUR_TRACK);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_DISTANCE);
	pControlPopup->SetBeginGroup(TRUE);
	
	pControlPopup = (CXTPControlPopup*)pGroupFile->Add(xtpControlSplitButtonPopup, ID_MEDIA_MORPHOLOGY);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_EROSION);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_DILATION);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_OPENNING);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_CLOSING);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_GRADS);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_GRAY_TOPHAT);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_GRAY_BLACKHAT);
	pControlPopup->SetBeginGroup(TRUE);
	
	pControlPopup = (CXTPControlPopup*)pGroupFile->Add(xtpControlSplitButtonPopup, ID_MEDIA_PICTURE);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_IMAGE_RGB);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_IMAGE_HSV);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_IMAGE_LAB);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_IMAGE_RGB_C);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_IMAGE_XYZ);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_IMAGE_YCRCB);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_IMAGE_LUV);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_2D_HISTOGRAM);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_BLUR_SMOOTH_C);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_GAUSS_SMOOTH_C);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_MEDIAN_SMOOTH_C);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_SOBEL_C);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_LAPLACE_C);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_PICTURE_AVERAGE);
	pControlPopup->SetBeginGroup(TRUE);
	
	pControlPopup = (CXTPControlPopup*)pGroupFile->Add(xtpControlSplitButtonPopup, ID_MEDIA_ALLDEAL);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_IMAGE_SHRINK);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_RADIAL_GRADS);
	pControlPopup->SetBeginGroup(TRUE);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_CANNY_BORDDETEC);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_HOUGH_LINES);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_HOUGH_CIRCLE);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_SQUARES_TEST);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_FLOOD_FILL);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_PYR_SEGMENTATION);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_FIT_ELLIPSE);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_SNAKE);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_WATERSHED);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_CORNERS_TEST);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_CLUSTERS);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_SEQ_PARTITION);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_ROTATION_TRACK);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_FACE_DETECT);
	pControlPopup->SetBeginGroup(TRUE);
	
	pControlPopup = (CXTPControlPopup*)pGroupFile->Add(xtpControlSplitButtonPopup, ID_DYNAMIC_DETECTION);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_DYNAMIC_BORDER_TEST);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_OPTICAL_FLOW_TRACK);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, IF_BACKGROUND_DETECT);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, IF_FOREGROUND_DETECT);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_MOTION_DETECT);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_CAM_SHIFT_DEMO);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_FACE_DETECT_2);
	pControlPopup->SetBeginGroup(TRUE);
	
	pControlPopup = (CXTPControlPopup*)pGroupFile->Add(xtpControlSplitButtonPopup, ID_PICTURE_DEAL);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_DRAWING);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_DELAUNAY);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_LOG_POLAR);
	pControlPopup->GetCommandBar()->GetControls()->Add(xtpControlButton, ID_DFT);
	pControlPopup->SetBeginGroup(TRUE);

	int count = pRibbonBar->GetTabCount();
	for(int i = 1 ; i< count; ++i)
	{
		CXTPRibbonTab* pTab = pRibbonBar->GetTab(i);
		if(pTab == pTabControl)
		{
			pRibbonBar->SetCurSel(i);
		}
	}
	pRibbonBar->EnableFrameTheme();
	return true;
}


void COpenCVFrm::LoadIcons()
{
	CXtremeMDIFrameWnd* pMainFrame = (CXtremeMDIFrameWnd*)AfxGetMainWnd();	
	CSysLocalResource res(theApp.m_hInstance);	
	CXTPCommandBars* pCommandBars = pMainFrame->GetCommandBars();
	pCommandBars->GetImageManager()->SetIcons(IDR_LARGE_TOOLBAR);
	//pCommandBars->GetImageManager()->SetIcons(IDR_SMALL_TOOLBAR);
	UINT mediaplay[] = {ID_MEDIA_PLAY, ID_MEDIA_STOP, ID_MEDIA_SHOW };
	pCommandBars->GetImageManager()->SetIcons(ID_MEDIA_PLAY, mediaplay, _countof(mediaplay), CSize(32, 32));
	//UINT mediastop[] = {ID_MEDIA_STOP, 1 };
	//pCommandBars->GetImageManager()->SetIcons(ID_MEDIA_STOP, mediaplay, _countof(mediaplay), CSize(256, 256));

}

//int COpenCVFrm::UpdateRibbonTab()
//{
//	//CXtremeMDIFrameWnd* pMainFrame = (CXtremeMDIFrameWnd*)AfxGetMainWnd();	
//	//if(pMainFrame == NULL)
//	//{
//	//	return 0;
//	//}
//	//CXtremChildFrame * pChildFrame = (CXtremChildFrame * )pMainFrame->MDIGetActive();
//	//if(pChildFrame == NULL)
//	//{
//	//	return 0;
//	//}
//	//if(!pChildFrame->IsKindOf(RUNTIME_CLASS(COpenCVFrm)))
//	//{
//	//	return 0;
//	//}
//	//if( pTabControl== NULL)
//	//{
//	//	return 0;
//	//}
//	//pTabControl->SetVisible(TRUE);
//	//int count = pRibbonBar->GetTabCount();
//	//for(int i = 1 ; i< count; ++i)
//	//{
//	//	CXTPRibbonTab* pTab = pRibbonBar->GetTab(i);
//	//	if(pTab == pTabControl)
//	//	{
//	//		pRibbonBar->SetCurSel(i);
//	//	}
//	//}
//	return 1 ;
//}
